package demo04.personOperation.daoImpl;

import demo03.hw4_25;
import demo04.personOperation.dao.PersonDao;
import demo04.personOperation.pojo.Person;
import demo04.personOperation.tool.ConnectionTool;
import demo04.personOperation.tool.Const;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PersonDaoImpl implements PersonDao {

    Connection conn = null;

    static {
        try {
            Class.forName(Const.DRIVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(List<Person> people) {

        PreparedStatement ps_insert = null;
        boolean result = true;

        try {
            conn = ConnectionTool.getDefaultConn();

            conn.setAutoCommit(false);

            String sql = "insert into people values(?,?,?,?,?,?)";

            ps_insert = conn.prepareStatement(sql);

            for (Person person : people) {
                ps_insert.setString(1, person.getPersonId());
                ps_insert.setString(2, person.getPersonName());
                ps_insert.setString(3, person.getDegree());
                ps_insert.setString(4, person.getBirth());
                ps_insert.setDouble(5, person.getSal());
                ps_insert.setString(6, "0");
                ps_insert.addBatch();
            }

            int[] insertCount = ps_insert.executeBatch();


            if (insertCount.length == people.size() && Arrays.stream(insertCount).allMatch(num -> num == 1)) {
                conn.commit();
            } else {
                result = false;
                conn.rollback();
            }
        } catch (SQLException e) {
            try {
                result = false;
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
        } finally {
            ConnectionTool.closed(conn, ps_insert);
        }
        return result;
    }


    @Override
    public boolean update(Person person) {

        PreparedStatement ps_update = null;

        try {
            conn = ConnectionTool.getDefaultConn();
            String sql = "update people set personName=?,degree=?,birth=?,sal=?,isdelete=? where personId=?";

            ps_update = conn.prepareStatement(sql);

            ps_update.setString(1, person.getPersonName());
            ps_update.setString(2, person.getDegree());
            ps_update.setString(3, person.getBirth());
            ps_update.setDouble(4, person.getSal());
            ps_update.setString(5, "0");
            ps_update.setString(6, person.getPersonId());
            int rows = ps_update.executeUpdate();
            if (rows > 0) {
                System.out.println("更新成功");
            } else {
                System.out.println("更新失败");
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            ConnectionTool.closed(conn, ps_update);
        }

        return false;
    }


    @Override
    public boolean delete(String[] ids) {

        PreparedStatement ps_delete = null;

        try {
            conn = ConnectionTool.getDefaultConn();
            String sql = "delete from people where personId=?";

            for (int i = 0; i < ids.length; i++) {
                ps_delete = conn.prepareStatement(sql);
                ps_delete.setString(1, ids[i]);
                int rows = ps_delete.executeUpdate();
                if (rows > 0) {
                    System.out.println("删除成功");
                } else {
                    System.out.println("用户不存在");
                }
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            ConnectionTool.closed(conn, ps_delete);
        }
        return false;
    }

    @Override
    public List<Person> getAll() {
        PreparedStatement ps_select = null;
        ResultSet rs = null;
        List<Person> people = new ArrayList<>();
        try {
            String sql = "select * from people";
            ps_select = conn.prepareStatement(sql);
            rs = ps_select.executeQuery();
            while (rs.next()) {
                people.add(new Person(
                        rs.getString("personId"),
                        rs.getString("personName"),
                        rs.getString("degree"),
                        rs.getString("birth"),
                        rs.getDouble("sal"),
                        rs.getString("isdelete")
                ));
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            ConnectionTool.closed(conn, ps_select, rs);
        }
        return people;
    }

    @Override
    public boolean login(String loginname, String userpwd) throws SQLException {
        return false;
    }

    @Override
    public List<Person> getUserForPage(int pagesize, int pageOrder) {
        return List.of();
    }

    @Override
    public Person getUserById(int id) {
        return null;
    }
}
